package com.pzl.palette;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<Fragment> fragments;
    private ViewPager viewPager;
    private Palette palette;
    private int[] imgRes = new int[]{R.drawable.a14, R.drawable.abc1};
    private ActionBar actionBar;
    private ScrollView scrollView;
    private LinearLayout linearLayout;

    private void initView() {
        FragmentA fragmentA = new FragmentA();
        FragmentB fragmentB = new FragmentB();
        fragments = new ArrayList<Fragment>();
        fragments.add(fragmentA);
        fragments.add(fragmentB);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        scrollView = (ScrollView) findViewById(R.id.sv);
        linearLayout = (LinearLayout) findViewById(R.id.ll);
        actionBar = getSupportActionBar();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        initView();
        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragments));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changeStyle(BitmapFactory.decodeResource(getResources(), imgRes[position]));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar page clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void changeStyle(Bitmap bitmap) {
        palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {

                linearLayout.removeAllViews();

                Palette.Swatch swatch = palette.getVibrantSwatch();
                scrollView.setBackgroundColor(swatch.getRgb());
                linearLayout.setBackgroundColor(swatch.getRgb());
                actionBar.setBackgroundDrawable(new ColorDrawable(swatch.getRgb()));

                swatch = palette.getMutedSwatch();

                TextView tv1 = new TextView(MainActivity.this);
                LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams1.bottomMargin = 20;
                tv1.setPadding(0, 10, 0, 10);
                tv1.setTextSize(22);
                tv1.setTextColor(Color.WHITE);
                tv1.setGravity(Gravity.CENTER);
                if (swatch != null) {
                    tv1.setBackgroundColor(swatch.getRgb());
                }
                tv1.setText("Muted:Rgb");
                linearLayout.addView(tv1, layoutParams1);

                swatch = palette.getDarkMutedSwatch();

                TextView tv2 = new TextView(MainActivity.this);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams2.bottomMargin = 20;
                tv2.setPadding(0, 10, 0, 10);
                tv2.setTextSize(22);
                tv2.setTextColor(Color.WHITE);
                tv2.setGravity(Gravity.CENTER);
                if (swatch != null) {
                    tv2.setBackgroundColor(swatch.getRgb());
                }
                tv2.setText("DarkMuted:Rgb");
                linearLayout.addView(tv2, layoutParams2);

                swatch = palette.getDarkVibrantSwatch();

                TextView tv3 = new TextView(MainActivity.this);
                LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams3.bottomMargin = 20;
                tv3.setPadding(0, 10, 0, 10);
                tv3.setTextSize(22);
                tv3.setTextColor(Color.WHITE);
                tv3.setGravity(Gravity.CENTER);
                if (swatch != null) {
                    tv3.setBackgroundColor(swatch.getRgb());
                }
                tv3.setText("DarkVibrant:Rgb");
                linearLayout.addView(tv3, layoutParams3);

                swatch = palette.getLightMutedSwatch();

                TextView tv4 = new TextView(MainActivity.this);
                LinearLayout.LayoutParams layoutParams4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams4.bottomMargin = 20;
                tv4.setPadding(0, 10, 0, 10);
                tv4.setTextSize(22);
                tv4.setTextColor(Color.WHITE);
                tv4.setGravity(Gravity.CENTER);
                if (swatch != null) {
                    tv4.setBackgroundColor(swatch.getRgb());
                }
                tv4.setText("LightMuted:Rgb");
                linearLayout.addView(tv4, layoutParams4);

            }
        });
    }
}
